<?php
/*
 * Project:     seositer
 * File:        cache.inc - this is a copy of RSS_CACHE with mild semantical changes
 * 				a simple, rolling(no GC), cache 
 *              for RSS objects, keyed on URL.
 * Author:      Kellan Elliott-McCrea <kellan@protest.net>
 * Version:     0.51
 * License:     GPL
 *
 * The lastest version of MagpieRSS can be obtained from:
 * http://magpierss.sourceforge.net
 *
 * For questions, help, comments, discussion, etc., please join the
 * Magpie mailing list:
 * http://lists.sourceforge.net/lists/listinfo/magpierss-general
 *
 */

class Cache {
    var $BASE_CACHE = './cache';    // where the cache files are stored
    var $MAX_AGE    = 600;         // when are files stale, default 10 minutes
    var $ERROR      = "";           // accumulate error messages
    
    function Cache ($base='', $age='') {
        if ( $base ) {
            $this->BASE_CACHE = $base;
        }
        if ( $age ) {
            $this->MAX_AGE = $age;
        }
        
        // attempt to make the cache directory
        if ( ! file_exists( $this->BASE_CACHE ) ) {
            $status = @mkdir( $this->BASE_CACHE, 0755 );
            
            // if make failed 
            if ( ! $status ) {
                $this->error(
                    "Cache couldn't make dir '" . $this->BASE_CACHE . "'."
                );
            }
        }
    }

/*=======================================================================*\
    Function:   revoke
    Purpose:    removes an item from the cache, keyed by name, if exists
    Input:      name of cache
    Output:     true on success, false on failure
\*=======================================================================*/
	function revoke($name)
	{
		try
		{
			$this->ERROR = "";
			$cache_file = $this->file_name( $name );
			if (file_exists($cache_file))
				unlink($cache_file);
			return true;
		}
		catch(Exception $ex)
		{
			return false;
		}
	}
    
/*=======================================================================*\
    Function:   set
    Purpose:    add an item to the cache, keyed on name
    Input:      name from which the rss file was fetched
    Output:     true on sucess  
\*=======================================================================*/
    function set ($name, $data) {
        $this->ERROR = "";
        $cache_file = $this->file_name( $name );
        $fp = @fopen( $cache_file, 'w' );
        
        if ( ! $fp ) {
            $this->error(
                "Cache unable to open file for writing: $cache_file"
            );
            return 0;
        }
        
        
        $serialized = $this->serialize( $data );
        fwrite( $fp, $serialized );
        fclose( $fp );
        
        return $cache_file;
    }
    
/*=======================================================================*\
    Function:   get
    Purpose:    fetch an item from the cache
    Input:      name from wich the rss file was fetched
    Output:     cached object on HIT, false on MISS 
\*=======================================================================*/ 
    function get ($name) {
        $this->ERROR = "";
        $cache_file = $this->file_name( $name );
        
        if ( ! file_exists( $cache_file ) ) {
            $this->debug( 
                "Cache doesn't contain: $name (cache file: $cache_file)"
            );
            return 0;
        }
        
        $fp = @fopen($cache_file, 'r');
        if ( ! $fp ) {
            $this->error(
                "Failed to open cache file for reading: $cache_file"
            );
            return 0;
        }
        
        if ($filesize = filesize($cache_file) ) {
        	$serialized = fread( $fp, filesize($cache_file) );
        	$data = $this->unserialize( $serialized );
        
        	return $data;
    	}
    	
    	return 0;
    }

/*=======================================================================*\
    Function:   check_cache
    Purpose:    check a url for membership in the cache
                and whether the object is older then MAX_AGE (ie. STALE)
    Input:      url from wich the rss file was fetched
    Output:     cached object on HIT, false on MISS 
\*=======================================================================*/     
    function check_cache ( $url ) {
        $this->ERROR = "";
        $filename = $this->file_name( $url );
        
        if ( file_exists( $filename ) ) {
            // find how long ago the file was added to the cache
            // and whether that is longer then MAX_AGE
            $mtime = filemtime( $filename );
            $age = time() - $mtime;
            if ( $this->MAX_AGE > $age ) {
                // object exists and is current
                return 'HIT';
            }
            else {
                // object exists but is old
                return 'STALE';
            }
        }
        else {
            // object does not exist
            return 'MISS';
        }
    }

	function cache_age( $cache_key ) {
		$filename = $this->file_name( $url );
		if ( file_exists( $filename ) ) {
			$mtime = filemtime( $filename );
            $age = time() - $mtime;
			return $age;
		}
		else {
			return -1;	
		}
	}
	
/*=======================================================================*\
    Function:   serialize
	Comment: added special treatment to xml objects, to fix 
	"node no exists" bug.
\*=======================================================================*/     
    function serialize ( $data ) 
	{
		if($data instanceof SimpleXMLElement)
		{
			$stdClass = new stdClass();
			$stdClass->type = get_class($data);
			$stdClass->data = $data->asXml();
			$data = $stdClass;
		}

        return serialize( $data );
    }

/*=======================================================================*\
    Function:   unserialize
\*=======================================================================*/     
    function unserialize ( $data ) 
	{
        $data = unserialize( $data );
		
		if($data instanceof stdClass && $data->type == "SimpleXMLElement")
		{
			$data = simplexml_load_string($data->data);
		}
		
		return $data;
    }
    
/*=======================================================================*\
    Function:   file_name
    Purpose:    map url to location in cache
    Input:      url from wich the rss file was fetched
    Output:     a file name
\*=======================================================================*/     
    function file_name ($url) {
        $filename = md5( $url );
        return join( DIRECTORY_SEPARATOR, array( $this->BASE_CACHE, $filename ) );
    }

/*=======================================================================*\
    Function:   error
    Purpose:    register error
\*=======================================================================*/         
    function error ($errormsg, $lvl=E_USER_WARNING) {
        // append PHP's error message if track_errors enabled
        if ( isset($php_errormsg) ) { 
            $errormsg .= " ($php_errormsg)";
        }
        $this->ERROR = $errormsg;
        if ( MAGPIE_DEBUG ) {
            trigger_error( $errormsg, $lvl);
        }
        else {
            error_log( $errormsg, 0);
        }
    }
    
    function debug ($debugmsg, $lvl=E_USER_NOTICE) {
        if ( MAGPIE_DEBUG ) {
            $this->error("MagpieRSS [debug] $debugmsg", $lvl);
        }
    }

}

?>
